package redisService

import (
	"backend/config"
	"fmt"
	"github.com/go-redis/redis"
	"time"
)

var ClientRedis *redis.Client

func Init() {
	ClientRedis = redis.NewClient(&redis.Options{
		Network:            REDIS_NETWORK,
		Addr:               fmt.Sprintf("%s:%s", config.Config.Redis.RedisHost, config.Config.Redis.RedisPort),
		Dialer:             nil,
		OnConnect:          nil,
		Password:           config.Config.Redis.RedisPassword,
		DB:                 REDIS_DB,
		MaxRetries:         0,
		MinRetryBackoff:    0,
		MaxRetryBackoff:    0,
		DialTimeout:        0,
		ReadTimeout:        0,
		WriteTimeout:       0,
		PoolSize:           0,
		MinIdleConns:       0,
		MaxConnAge:         0,
		PoolTimeout:        0,
		IdleTimeout:        0,
		IdleCheckFrequency: 0,
		TLSConfig:          nil,
	})
}

//var ClientRedis *redis.Client = redis.NewClient(&redis.Options{
//	Network:            REDIS_NETWORK,
//	Addr:               fmt.Sprintf("%s:%s", REDIS_HOST, REDIS_PORT),
//	Dialer:             nil,
//	OnConnect:          nil,
//	Password:           REDIS_PASSWORD,
//	DB:                 REDIS_DB,
//	MaxRetries:         0,
//	MinRetryBackoff:    0,
//	MaxRetryBackoff:    0,
//	DialTimeout:        0,
//	ReadTimeout:        0,
//	WriteTimeout:       0,
//	PoolSize:           0,
//	MinIdleConns:       0,
//	MaxConnAge:         0,
//	PoolTimeout:        0,
//	IdleTimeout:        0,
//	IdleCheckFrequency: 0,
//	TLSConfig:          nil,
//})

const (
	REDIS_NETWORK = "tcp"
	//REDIS_HOST    = "82.156.206.110"
	//REDIS_HOST = "zjx_redis"
	//REDIS_PORT     = "4103"
	//REDIS_PORT     = "6379"
	//REDIS_PASSWORD = "990812"
	REDIS_DB = 0
)

func GetIdByToken(token string) (bool, string) {
	if token == "" {
		return false, ""
	}
	result, err := ClientRedis.Get(token).Result()
	if err != nil {
		return false, ""
	}
	return true, result
}

func SetToken(id string, rem bool, s string) string {

	duration := time.Hour * 24
	if rem {
		duration = time.Hour * 7 * 24
	}
	ClientRedis.Set(s, id, duration)
	return s
}
